package com.s69.demo.controller;

import com.s69.demo.entity.AppInfo;
import com.s69.demo.entity.AppVersion;
import com.s69.demo.entity.DevUser;
import com.s69.demo.service.AppInfoService;
import com.s69.demo.service.VersionService;
import com.s69.demo.utils.SessionUtils;
import com.s69.demo.utils.SysConstant;
import com.s69.demo.vo.ResultVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/version")
public class VersionController {

    @Autowired
    VersionService versionService;
    @Autowired
    AppInfoService appInfoService;

    @GetMapping("/selectVersion")
    public ResultVo selectVersion(Long appid) {
        List<AppVersion> appVersions = versionService.list(appid);
        return ResultVo.success("", appVersions);
    }

    @PostMapping("/saveOrUpdate")
    public ResultVo saveOrUpdate(AppVersion appVersion, MultipartFile apkFile, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (apkFile != null && !apkFile.isEmpty()) {
            String originalFilename = apkFile.getOriginalFilename();
            String fileFormat = FilenameUtils.getExtension(originalFilename);
            if (apkFile.getSize() > 500 * 1024) {
                return ResultVo.error("图片大小超过500k");
            } else if (fileFormat.equals("apk")) {
                String realPath = request.getServletContext().getRealPath("upload/apk");
                File savePath = new File(realPath);
                if (!savePath.exists()) {
                    savePath.mkdirs();
                }
                String fileName = UUID.randomUUID().toString().replace("-", "");
                File saveFile = new File(realPath + "/" + fileName + "." + fileFormat);
                try {
                    apkFile.transferTo(saveFile);
                    appVersion.setDownloadlink("upload/apk/" + fileName + "." + fileFormat);
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultVo.error("logo图片上传失败");
                }
            } else {
                return ResultVo.error("文件格式不正确");
            }
        }

        DevUser devUser = (DevUser) SessionUtils.get(token);
        if (appVersion.getId() == null) {
            appVersion.setPublishstatus(SysConstant.AppStatus.pre);
            appVersion.setCreatedby(devUser.getId());
            appVersion.setCreationdate(new Date());
        } else {
            appVersion.setModifyby(devUser.getId());
            appVersion.setModifydate(new Date());
        }
        versionService.saveOrUpdate(appVersion);

        AppInfo appInfo = new AppInfo();
        appInfo.setId(appVersion.getAppid());
        appInfo.setVersionid(appVersion.getId());
        appInfo.setStatus(1L);
        appInfoService.saveOrUpdate(appInfo);

        return ResultVo.success("", null);
    }

}
